$(document).ready(function() {
    var __addForm = $('#form_add'),
        __editForm = $('#form_edit');
    $('#submit_form').click(function() {
        var objForm = {
            __formName: $("input[name='name']", __addForm),
            __formSource: $("input[name='source']", __addForm),
            __formArticleType: $("select[name='article_type_id']", __addForm),
            __formAddress: $("input[name='coll_address']", __addForm),
            __formRobot: $("select[name='coll_robot']", __addForm),
            __formRemark: $("textarea[name='remark']", __addForm)
        }

        if (0 == objForm.__formName.val().length) {
            window.alert('采集源名称不能为空！');
            return false;
        }

        if (0 == objForm.__formSource.val().length) {
            window.alert('采集源不能为空！');
            return false;
        }

        $.ajax({
            type: "POST",
            url: "/Source/add",
            data: {
                "name": objForm.__formName.val(),
                "source": 　objForm.__formSource.val(),
                "article_type_id": 　objForm.__formArticleType.val(),
                "robot_id": objForm.__formRobot.val(),
                "url": objForm.__formAddress.val(),
                "remark": 　objForm.__formRemark.val()
            },
            success: function(data) {

                layer.alert(data.info, function() {
                    if (true === data.status) window.location = "/Source/list";
                });


            }
        });

    });

    //$('.itemDelete').click(function(){
    $(document).on('click', '.tableDelete', function() {
        var userId = $(this).parent('td').attr('item_id'),
            thisUrl = $(this).attr('href');
        //官网欢迎页   
        layer.confirm('确认删除', {
            btn: ['确认', '取消'] //按钮
        }, function(index) {

            $.ajax({
                type: "GET",
                url: thisUrl,
                async: true,
                data: {
                    //"id" : userId
                },
                success: function(data) {
                    if (true === data.status) {
                        layer.msg('删除成功！');
                        window.location = "/Source/list";
                    }
                }
            });
            layer.close(index);
            return false;

        }, function(index) {
            layer.close(index);
        });
        return false;

    });


    //$('.itemEdit').click(function(e){
    $(document).on('click', '.tableEdit', function(e) {
        $('#form_edit').modal("toggle");
        item_id = $(this).parent('td').attr('item_id');
        current_status = $(this).parent('td').attr('status');
        current_type = $(this).parent('td').attr('article_type_id');
        current_robot = $(this).parent('td').attr('robot_id');

        var __parent = $(this).parent('td').parent('tr.gradeX'),
            __formName = $("input[name='name']", __editForm),
            __formSource = $("input[name='source']", __editForm),
            __formAddress = $("input[name='coll_address']", __editForm),
            __formArticleType = $("select[name='article_type_id']", __editForm),
            __formRobot = $("select[name='robot_id']", __editForm),
            __formRemark = $("textarea[name='remark']", __editForm);
        var tdTable = [];
        $('td', __parent).each(function() {
            tdTable.push($(this).text());
        });
        __formName.val(tdTable[0]);
        __formSource.val(tdTable[1]);
        __formAddress.val(tdTable[3]);
        __formArticleType.val(current_type);
        __formRobot.val(current_robot);
        __formRemark.val(tdTable[5]);

        return false;
    });


    $('#edit_form').click(function() {
        var objForm = {
            __formName: $("input[name='name']", __editForm),
            __formSource: $("input[name='source']", __editForm),
            __formAddress: $("input[name='coll_address']", __editForm),
            __formArticleType: $("select[name='article_type_id']", __editForm),
            __formRobot: $("select[name='robot_id']", __editForm),
            __formRemark: $("textarea[name='remark']", __editForm)
        }

        if ('' == objForm.__formName.val()) {
            window.alert('采集源名称不能为空！');
            return false;
        }

        if ('' == objForm.__formSource.val()) {
            window.alert('采集源不能为空！');
            return false;
        }

        $.ajax({
            type: "POST",
            url: "/Source/edit/id/" + item_id,
            data: {
                "name": objForm.__formName.val(),
                "source": 　objForm.__formSource.val(),
                "article_type_id": 　objForm.__formArticleType.val(),
                "robot_id": objForm.__formRobot.val(),
                "url": objForm.__formAddress.val(),
                "remark": 　objForm.__formRemark.val()
            },
            success: function(data) {
                layer.alert(data.info, function() {
                    if (true === data.status) window.location = "/Source/list";
                });
            }
        });

    })

    $(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
        var urlOld = $(this).attr('href'),
            urlArr = urlOld.split("/");
        urlId = urlArr[urlArr.length - 1];
        $.ajax({
            type: 'POST',
            url: '/Source/page/page/' + urlId,
            data: urlId,
            success: function(data) {
                var data = $.parseJSON(data);

                $('#tbody').html('');
                var id = '';

                $.each(data, function(k, v) {
                    //console.log('k='+k);
                    id = v['source_id'];
                    article_type_id = v['article_type_id'];
                    robot_id = v['robot_id'];

                    $('#tbody').append($('<tr class="gradeX"><td>' + v['name'] + '</td><td>' + v['source'] + '</td><td>' + v['typical_name'] + '</td><td>' + v['url'] + '</td><td>' + v['robot_name'] + '</td><td>' + v['remark'] + '</td><td>' + v['current_time'] + '</td><td item_id="' + v['id'] + '" article_type_id="' + article_type_id + '"  robot_id="' + robot_id + '"><a class="tableDelete" href="/Source/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/Source/edit/id/' + id + '">修改</a> </td></tr>'));


                });


            }

        });
        $.ajax({
            type: 'POST',
            url: '/Source/currentPage/page/' + urlId,
            data: urlId,
            success: function(data) {
                //console.log(data);
                $('#pages').html(data);
                $('#pages').ready(function() {
                    nextBtn = $('#page_next');
                });
            }

        });
        return false;
    });

});